define(["animate","mySelect"],function(animate,$){
	return function Swiper(ele , options){
	let oriOptions = {
		left : false,  //向左轮播
		top : false, //向上轮播
		opacity : false,  //透明度
		loop : true, //是否是循环
		times : 3000
	}
	
	//获取元素
	this.getElement = function(){
		this.newArr = Object.assign(oriOptions,options);	
		this.content = $(ele); //图片的父元素，图片和小圆点同级 ul
		this.imgs = Array.from(this.content.children); 
		this.olList = this.content.nextElementSibling; //拿到ol
		this.circles = Array.from(this.olList.children); //ol下的li
		this.currentIndex = 0;  //定义下标
		this.t = null; //需要给定时器设置一个变量，共用一个变量
		//左右两个的按钮
		this.btnLeft = $("#btn-left");
		this.btnRight = $("#btn-right");
	}
	
	//判断是否有定位
	this.isPosition = function(){
		if(getComputedStyle(this.content).position=="static"){
			this.content.style.position = "absolute"; //如果盛放图片的容器没有定位
		}
		this.olList.style.position = "absolute" ; //为ol设置定位
	}
	
	//图片播放方式
	this.methodPlay = function(){
		this.imgs.forEach(function(item){ //清除图片的定位，因为left和top中图片不需要定位
			item.style.position = "static";
		});
		if(this.newArr.left){
			//console.log(parseInt(getComputedStyle(this.content.parentNode).width)*this.imgs.length);
			//获取ul父元素的宽度，再乘li的个数
			this.content.style.width = parseInt(getComputedStyle(this.content.parentNode).width)*this.imgs.length + "px";
			return "left";
		} 
		if(this.newArr.top){
		//	console.log("top");
			this.content.style.height = parseInt(getComputedStyle(this.content.parentNode).height)*this.imgs.length + "px";
			return "top";
		}
		if(this.newArr.opacity){
		//	console.log("opacity");
			this.imgs.forEach(function(item){ //修改每张图片的定位，li都重叠在一起
				item.style.position = "absolute";
			});
			return "opacity";
		}
		
	}
	
	//计算下标
	this.getCurrentIndex = function(){
		let m = this.methodPlay();  //得到播放的方向
		this.currentIndex ++;
		if(this.currentIndex == this.imgs.length){//判断是否下标越界
			if(m == "left" || m == "top"){
				console.log(this.content.style[m]);
				this.content.style[m] = 0;
				this.currentIndex = 1;
			}
			this.currentIndex = 0;
		} 
	}
	
	//图片切换
	this.tab = function(){
		let m = this.methodPlay();  //得到播放的方向
		if(m == "left" ){  //左右切换
			//console.log(this.content);
			//console.log(-this.currentIndex*parseInt(getComputedStyle(this.content.parentNode).width));
			animate(this.content,{left:-this.currentIndex*parseInt(getComputedStyle(this.content.parentNode).width)})
		}else if(m == "top" ){ //上下切换
			animate(this.content,{top:-this.currentIndex*parseInt(getComputedStyle(this.content.parentNode).height)})
		}else{  //透明度
			animate(this.imgs[this.currentIndex],{opacity:10});
			this.imgs.forEach(function(item){  //让每个图片的透明度设为0
				item.style.opacity = 0;	
			});
		}
		
		//小圆点的操作
		for(let i =0 ;i<this.circles.length;i++){
			if(this.currentIndex == this.circles.length){
				this.circles[0].className = "circle-active";
			}
			if(i == this.currentIndex){
				this.circles[i].className = "circle-active";
			}else{
				this.circles[i].className = ""; //默认小圆点不亮
			}
		}
	}
	
	//鼠标划过暂停播放
	this.hoverTab = function(){
		this.content.addEventListener("mouseover",()=>{ 
			clearInterval(this.t);
		});
		this.content.addEventListener("mouseout",()=>{
			this.t = setInterval(()=>{
				this.getCurrentIndex();
				this.tab();
			},this.newArr.times);
		});
	}
	
	//左边点击
	this.leftClick = function(){
		this.btnLeft.addEventListener("click",()=>{
			this.currentIndex--;
			if(this.currentIndex == -1){ //如果下标为-1
				this.currentIndex = this.circles.length-1;
			}
			this.tab();
		})
	}
	//右边点击
	this.rightClick = function(){
		this.btnRight.addEventListener("click",()=>{
			this.currentIndex++;
			if(this.currentIndex == this.circles.length){ //如果下标为-1
				this.currentIndex = 0;
			}
			this.tab();
		})
	}
	
	//点击切换上一张或者下一张
	this.clickTab = function(){
		this.leftClick();
		this.rightClick();
	}
	
	//鼠标滑过切换图片
	this.hoverCircleTab = function(){
		this.circles.forEach((circle,index)=>{
			circle.addEventListener("mouseover",()=>{
					//console.log(index);
					this.currentIndex = index; //记录滑过的下标
					//clearInterval(this.t);
					this.tab();
				})
		})
	}
	
	
	//开始轮播
	this.imgPlay = function(){
		this.getElement();
		this.isPosition();
		this.hoverTab(); //鼠标经过停止，因为和正常播放的轮播图用公共的setinterval编号
		this.clickTab();
		this.hoverCircleTab();
		this.t = setInterval(()=>{
			this.getCurrentIndex();
			this.tab();
		},this.newArr.times);
	}
	
	//执行
//	this.getElement();
	//let m = this.methodPlay();
	this.imgPlay();
	
	
}
})
